<%@ page pageEncoding="UTF-8"%>
<%!
    class MyTask {
        // 开始的倒数锁
        private CountDownLatch begin = null;
        // 结束的倒数锁
        private CountDownLatch end = null;
        private ExecutorService executor = null;
        private Object[] result;

        public MyTask(int taskNum) {
            super();
            begin = new CountDownLatch(1);
            end = new CountDownLatch(taskNum);
            executor = Executors.newFixedThreadPool(taskNum <= 0 ? 1 : taskNum);
            result = new Object[taskNum];
        }

        public void runTask(Runnable runnable) {
            executor.submit(runnable);
        }

        public void stopTask() {
            begin.countDown();
            try {
                end.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            executor.shutdown();
        }

        public Object[] getResult() {
            return result;
        }

        public abstract class  DBOperate {
            public abstract Object operate(Object... objects);
        }

        class MyRunnable implements Runnable {
            private DBOperate operate;
            private Object[] objects;
            private int index;

            public MyRunnable(DBOperate operate, int index, Object... objects) {
                this.operate = operate;
                this.objects = objects;
                this.index = index;
            }

            @Override
            public void run() {
                try {
                    begin.await();
                    result[index] = operate.operate(objects);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    end.countDown();
                }
            }
        }
    }
%>
